package JAVACORE_TRAINING.ADVANCED.SWING.DropDrag;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ComboBoxTutorial extends JFrame {

	private JPanel contentPane;
	private JComboBox cbBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ComboBoxTutorial frame = new ComboBoxTutorial();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ComboBoxTutorial() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		cbBox = new JComboBox();
		cbBox.setMaximumRowCount(4);
		contentPane.add(cbBox, BorderLayout.NORTH);
		
		JButton btnButton = new JButton("Button");
		btnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cbBox.getSelectedItem();
				JOptionPane.showMessageDialog(null, "Value selected: " + cbBox.getSelectedItem());
			}
		});
		contentPane.add(btnButton, BorderLayout.SOUTH);
		initVlueComboBox();
	}
	
	private void initVlueComboBox(){
		DefaultComboBoxModel boxModel = new DefaultComboBoxModel();
		boxModel.addElement("Viet");
		boxModel.addElement("Sang");
		boxModel.addElement("Quan");
		boxModel.addElement("Vu");
		boxModel.addElement("Duc");
		boxModel.addElement("Vi");
		cbBox.setModel(boxModel);
	}

}
